<?php
define ( 'IN_UCHOME', '1' );
include_once '../common.php';

// 删除图片
function delpic($picid) {
	global $_SGLOBAL, $arr;
	if (! is_numeric ( $picid )) {
		return;
	}
	$query = $_SGLOBAL ['db']->query ( "SELECT * FROM " . tname ( 'pic' ) . " where picid=$picid" );
	if ($value = $_SGLOBAL ['db']->fetch_array ( $query )) {
		$_SGLOBAL ['db']->query ( "delete FROM " . tname ( 'pic' ) . " where picid=$picid" );
		$value ['src'] = $_SC ['attachdir'] . $value ['filepath'];
		! @unlink ( $value ['src'] );
		$value ['thumbArr'] = unserialize ( $value ['thumb'] );
		foreach ( $value ['thumbArr'] as $key => $thumb ) {
			$file = $_SC ['attachdir'] . $value ['filepath'] . "." . $key . ".jpg";
			if (file_exists ( $file )) {
				! @unlink ( $file );
			}
		}
		$arr ['status'] = 1;
	} else {
		$arr ['err'] .= " picid:{$picid} not exists.\n";
	}
}

$email = $_REQUEST ['e'];
$pwmd5 = md5 ( "sess_picsave_" . $_REQUEST ['p'] . "y" );

$op = $_REQUEST ['op'];

if ($op == "checklogin") {
	$auth = checkAuth ( $email, $pwmd5 );
	echo json_encode ( $auth );
	exit ();
}

if ($op == "logout") {
	session_destroy ();
	echo 1;
	exit ();
}
// 以下操作需要验证是否登录
if (! isset ( $_SESSION ['user'] ) || empty ( $_SESSION ['user'] )) {
	die ( "login please!" );
}
$user = $_SESSION ['user'];
$_SGLOBAL ['supe_uid'] = $user ['uid'];
if ($op == "delpic") {
	
	$picid = empty ( $_POST ['picid'] ) ? "" : $_POST ['picid'];
	// print_r($picid);exit;
	$arr = array (
			"status" => 0 
	);
	if (empty ( $picid )) {
		$arr ['err'] = "picid null";
		echo json_encode ( $arr );
		exit ();
	}
	
	if (is_numeric ( $picid )) {
		delpic ( $picid );
	} else if (is_array ( $picid )) {
		foreach ( $picid as $v ) {
			delpic ( $v );
		}
	}
	
	echo json_encode ( $arr );
	exit ();
}
if ($op == "getpic") {
	$start = empty ( $_GET ['start'] ) ? '' : intval ( $_GET ['start'] );
	// 分页
	$perpage = 1;
	
	$list = array ();
	
	$page = empty ( $_GET ['page'] ) ? 1 : intval ( $_GET ['page'] );
	if ($page < 1) {
		$page = 1;
	}
	$count = getcount ( "pic" );
	
	$email = $_REQUEST ['e'];
	$type = $_REQUEST ['type'] ? $_REQUEST ['type'] : "normal";
	$pageSize = intval ( $_REQUEST ['pageSize'] ) ? $_REQUEST ['pageSize'] : "24";
	
	$theurl = "index.php?e={$email}&p={$_GET['pp']}&type=$type";
	
	$multi = multi ( $count, $pageSize, $page, $theurl );
	
	if (empty ( $start )) {
		$result = array (
				'data' => $list,
				'pagination' => $multi 
		);
		echo json_encode ( $result );
		exit ();
	}
	$query = $_SGLOBAL ['db']->query ( "SELECT * FROM " . tname ( 'pic' ) . " order by picid  LIMIT $start,$perpage" );
	while ( $value = $_SGLOBAL ['db']->fetch_array ( $query ) ) {
		$value ['src'] = $_SC ['attachurl'] . $value ['filepath'];
// 		$value ['thumbArr'] = unserialize ( $value ['thumb'] );
// 		$ext = $value ['thumbArr'] ['800'] ? ".800.jpg" : "";
// 		$value ['800'] = $value ['src'] . $ext;
// 		$thumbext = $value ['thumbArr'] ['thumb'] ? ".thumb.jpg" : "";
// 		$value ['thumb'] = $value ['src'] . $thumbext;
		$value ['dateline'] = sgmdate ( "Y-m-d", $value ['dateline'] );
		$list [] = $value;
	}
	$result = array (
			'data' => $list,
			'pagination' => $multi 
	);
	echo json_encode ( $result );
	exit ();
}

?>